/*
 * Created by JFormDesigner on Wed Oct 22 17:01:20 CST 2025
 */

package com.geek.bams.client;

import com.geek.bams.entity.Account;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RegisterPanel extends JPanel {

    private ATMClient atm;

    public RegisterPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    //返回至主页面
    private void back(ActionEvent e) {
        // 清空所有输入框
        clearAllFields();
        
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        cardLayout.show(this.getParent(), "mainPanel");
    }

    //实现完整的注册功能
    private void submit(ActionEvent e) {
        // 获取用户输入
        String accountType = (String) comboBox.getSelectedItem();
        String userName = nameTextField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();
        String repassword = new String(repasswordField.getPassword()).trim();
        String personId = personIdTextField.getText().trim();
        String address = addrTextField.getText().trim();
        String email = emailTextField.getText().trim();
        
        // 输入验证
        if (userName.isEmpty() || password.isEmpty() || personId.isEmpty() || email.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        // 密码确认验证
        if (!password.equals(repassword)&&!password.matches("^\\d{6,}$")) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致！", "密码错误", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        // 密码长度验证  6位数字校验
        if (!password.matches("^\\d{6,}$")) {
            JOptionPane.showMessageDialog(this, "密码长度不能少于6位！", "密码错误", JOptionPane.WARNING_MESSAGE);
            return;
        }
        // 身份证格式简单验证 使用正则表达式
        if ( !personId.matches("^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$")) {
            JOptionPane.showMessageDialog(this, "身份证号格式错误，请修改！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }//
        
        // 邮箱格式简单验证
        if (!email.contains("@") || !email.contains(".")) {
            JOptionPane.showMessageDialog(this, "邮箱格式错误！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        try {
            // 根据账户类型确定type参数
            int type;
            switch (accountType) {
                case "储蓄账户":
                    type = 0;
                    break;
                case "信用账户":
                    type = 1;
                    break;
                case "可贷款的储蓄账户":
                    type = 2;
                    break;
                case "可贷款的信用账户":
                    type = 3;
                    break;
                default:
                    type = 0;
                    break;
            }
            
            // 通过网络通信服务进行注册
            Object result = atm.clientService.register(password, repassword, userName, personId, email, type);
            
            // 判断返回结果类型
            if (result instanceof Account) {
                // 注册成功，保存账户信息和密码
                Account account = (Account) result;
                atm.acc = account;
                atm.currentPassword = password;
                
                // 清空输入框
                clearAllFields();
                
                // 显示成功消息，包含账户ID
                JOptionPane.showMessageDialog(this, 
                    "注册成功！\n您的账户ID是：" + account.getId() + "\n请牢记您的账户ID用于登录", 
                    "注册成功", JOptionPane.INFORMATION_MESSAGE);
                
                // 跳转至业务界面
                CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
                cardLayout.show(this.getParent(), "businessPanel");
            } else if (result instanceof String) {
                // 注册失败，显示错误信息
                JOptionPane.showMessageDialog(this, (String) result, "注册失败", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "未知错误：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    // 清空所有输入框的辅助方法
    private void clearAllFields() {
        comboBox.setSelectedIndex(0);
        nameTextField.setText("");
        passwordField.setText("");
        repasswordField.setText("");
        personIdTextField.setText("");
        addrTextField.setText("");
        emailTextField.setText("");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        titleLabel = new JLabel();
        comboBox = new JComboBox<>();
        nameTextField = new JTextField();
        personIdTextField = new JTextField();
        addrTextField = new JTextField();
        emailTextField = new JTextField();
        passwordField = new JPasswordField();
        repasswordField = new JPasswordField();
        submitButton = new JButton();
        backButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("\u8d26 \u6237 \u7c7b \u578b");
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        add(label1);
        label1.setBounds(80, 120, 115, 40);

        //---- label2 ----
        label2.setText("\u7528  \u6237  \u540d");
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        add(label2);
        label2.setBounds(80, 165, 115, 40);

        //---- label3 ----
        label3.setText("\u5bc6        \u7801");
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        add(label3);
        label3.setBounds(80, 215, 115, 40);

        //---- label4 ----
        label4.setText("\u786e \u8ba4 \u5bc6 \u7801");
        label4.setHorizontalAlignment(SwingConstants.CENTER);
        add(label4);
        label4.setBounds(80, 260, 115, 40);

        //---- label5 ----
        label5.setText("\u8eab \u4efd \u8bc1 \u53f7");
        label5.setHorizontalAlignment(SwingConstants.CENTER);
        add(label5);
        label5.setBounds(80, 315, 115, 40);

        //---- label6 ----
        label6.setText("\u8be6 \u7ec6 \u5730 \u5740");
        label6.setHorizontalAlignment(SwingConstants.CENTER);
        add(label6);
        label6.setBounds(80, 360, 115, 40);

        //---- label7 ----
        label7.setText("EMail");
        label7.setHorizontalAlignment(SwingConstants.CENTER);
        add(label7);
        label7.setBounds(80, 405, 115, 40);

        //---- titleLabel ----
        titleLabel.setText("\u6ce8  \u518c  \u754c  \u9762");
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        add(titleLabel);
        titleLabel.setBounds(105, 35, 275, 65);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u50a8\u84c4\u8d26\u6237",
                "\u4fe1\u7528\u8d26\u6237",
                "\u53ef\u8d37\u6b3e\u7684\u50a8\u84c4\u8d26\u6237",
                "\u53ef\u8d37\u6b3e\u7684\u4fe1\u7528\u8d26\u6237"
        }));
        add(comboBox);
        comboBox.setBounds(200, 120, 155, 40);
        add(nameTextField);
        nameTextField.setBounds(200, 165, 155, 40);
        add(personIdTextField);
        personIdTextField.setBounds(200, 305, 155, 40);
        add(addrTextField);
        addrTextField.setBounds(200, 355, 155, 40);
        add(emailTextField);
        emailTextField.setBounds(200, 400, 155, 40);
        add(passwordField);
        passwordField.setBounds(200, 215, 155, 40);
        add(repasswordField);
        repasswordField.setBounds(200, 260, 155, 40);

        //---- submitButton ----
        submitButton.setText("\u63d0\u4ea4");
        submitButton.addActionListener(e -> submit(e));
        add(submitButton);
        submitButton.setBounds(130, 485, 78, 34);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(265, 485, 78, 34);

        setPreferredSize(new Dimension(475, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel titleLabel;
    private JComboBox<String> comboBox;
    private JTextField nameTextField;
    private JTextField personIdTextField;
    private JTextField addrTextField;
    private JTextField emailTextField;
    private JPasswordField passwordField;
    private JPasswordField repasswordField;
    private JButton submitButton;
    private JButton backButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
